package main

import (
	"fmt"
	"myapp/dal/model"
	"myapp/dal/query"
	"myapp/pkg"
	"strings"

	"github.com/spf13/cobra"
)

var (
	// 接收端口号
	//port string

	serverCmd = &cobra.Command{
		Use:   "server",
		Short: "启动http服务,使用方法: app server --port=?",
		Run: func(cmd *cobra.Command, args []string) {
			// if port == "" {
			// 	log.Fatalf("port参数不能为空")
			// }
			goStart()
		},
	}

	name string

	roleCmd = &cobra.Command{
		Use:   "role",
		Short: "角色操作",
	}

	// 添加用户子命令
	addRoleCmd = &cobra.Command{
		Use:   "add",
		Short: "添加角色: role add --name=?",
		Run: func(cmd *cobra.Command, args []string) {
			query.SetDefault(db)

			role := model.Role{
				Name: name,
			}

			query.Role.Create(&role)
			fmt.Println("添加角色: ", name)
		},
	}

	// 删除用户子命令
	delRoleCmd = &cobra.Command{
		Use:   "del",
		Short: "删除角色: role del --name=?",
		Run: func(cmd *cobra.Command, args []string) {
			query.SetDefault(db)
			rs, err := query.Role.Where(query.Role.Name.Eq(name)).Delete()
			fmt.Println("删除角色: ", name, rs, err)
		},
	}

	// 添加用户子命令
	flushEnCmd = &cobra.Command{
		Use:   "flushall",
		Short: "刷新英文: flushall?",
		Run: func(cmd *cobra.Command, args []string) {
			query.SetDefault(db)

			rs, _, err := query.Industry.Where().FindByPage(0, 10000)
			if err != nil {
				fmt.Println(err)
			}

			var querys []string
			var querys1 []string
			for _, v := range rs {
				//fmt.Println(v.ID, v.Name, v.Desc)
				if len(v.Name) > 0 && v.NameEn == "" {
					querys = append(querys, v.Name)
				}
				if len(v.Desc) > 0 && v.DescEn == "" {
					querys1 = append(querys1, v.Desc)
				}
			}

			if len(querys) > 0 {
				queryStr := strings.Join(querys, "\n")

				// 执行翻译
				result, err := pkg.Translate(queryStr)
				if err != nil {
					fmt.Println("Error:", err)
					return
				}

				// 打印翻译结果
				fmt.Println("翻译结果:", result)

				for k, v := range result.TransResult {
					fmt.Println(k, v.Src, v.Dst)
					query.Industry.Where(query.Industry.Name.Eq(v.Src)).UpdateColumn(query.Industry.NameEn, v.Dst)
				}

			}

			if len(querys1) > 0 {
				queryStr := strings.Join(querys1, "\n")

				// 执行翻译
				result, err := pkg.Translate(queryStr)
				if err != nil {
					fmt.Println("Error:", err)
					return
				}

				// 打印翻译结果
				fmt.Println("翻译结果:", result)

				for k, v := range result.TransResult {
					fmt.Println(k, v.Src, v.Dst)
					query.Industry.Where(query.Industry.Desc.Eq(v.Src)).UpdateColumn(query.Industry.DescEn, v.Dst)
				}

			}
			//fmt.Println(rs, err)

			rs1, _, err := query.Category.Where().FindByPage(0, 10000)
			if err != nil {
				fmt.Println(err)
			}

			var cat_querys []string
			for _, v := range rs1 {
				//fmt.Println(v.ID, v.Name, v.Desc)
				if len(v.Name) > 0 && v.NameEn == "" {
					cat_querys = append(cat_querys, v.Name)
				}
			}

			if len(cat_querys) > 0 {
				queryStr := strings.Join(cat_querys, "\n")

				// 执行翻译
				result, err := pkg.Translate(queryStr)
				if err != nil {
					fmt.Println("Error:", err)
					return
				}

				// 打印翻译结果
				fmt.Println("翻译结果:", result)

				for k, v := range result.TransResult {
					fmt.Println(k, v.Src, v.Dst)
					query.Category.Where(query.Category.Name.Eq(v.Src)).UpdateColumn(query.Category.NameEn, v.Dst)
				}

			}
		},
	}
)

func init() {
	// 将server命令添加为rootCmd的子命令
	rootCmd.AddCommand(serverCmd)

	rootCmd.AddCommand(flushEnCmd)

	rootCmd.AddCommand(roleCmd)
	roleCmd.AddCommand(addRoleCmd)
	roleCmd.AddCommand(delRoleCmd)

	// server子命令接收port选项参数
	//serverCmd.Flags().StringVar(&port, "port", "", "端口号")
	roleCmd.PersistentFlags().StringVarP(&name, "name", "n", "", "角色名")
}
